package com.yoomiito.app.ui.anyview.gasstation

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
//import com.amap.api.maps2d.CoordinateConverter
import com.yoomiito.app.R
import com.yoomiito.app.distanceFormat
import com.yoomiito.app.model.LatLng
import com.yoomiito.app.model.oil.OilNum
import com.yoomiito.app.model.oil.OilStationInfo
import com.yoomiito.app.utils.ColorUtils
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.widget.dialog.SelectMapDialog
import kotlinx.android.synthetic.main.view_gas_station_card.view.*
import kotlin.math.ln

class GasStationCardView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    private lateinit var info: OilStationInfo
    val view: View = LayoutInflater.from(context).inflate(R.layout.view_gas_station_card, null, false).also {
        it.findViewById<LinearLayout>(R.id.navigation).let { nav ->
            nav.background = ColorUtils.gradientAndRadius("#5CB331", "#5CB331", 100f, 0f, 100f, 0f)
            nav.setOnClickListener {
                /*
                    易加油 type=1 坐标为百度坐标
                    团油 type=2 坐标为火星坐标系
                    小桔 type=3 坐标为火星坐标系
                */
                if (info.type == 1) {
//                    val converter = CoordinateConverter()
//                    // CoordType.GPS 待转换坐标类型
//                    converter.from(CoordinateConverter.CoordType.BAIDU)
//                    // sourceLatLng待转换坐标点 LatLng类型
//                    converter.coord(com.amap.api.maps2d.model.LatLng(info.lat.toDouble(), info.lng.toDouble()))
//                    // 执行转换操作
//                    val latlng: com.amap.api.maps2d.model.LatLng = converter.convert()
//                    selectMap(latlng.latitude, latlng.longitude)
                } else {
                    selectMap(info.lat.toDouble(), info.lng.toDouble())
                }
            }
        }
    }

    init {
        val lp = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        addView(view, lp)
    }

    fun initInfo(info: OilStationInfo) {
        this.info = info
        ImageLoader.getInstance().loadGaussianBlur(context, 80, info.logo, bg)
        ImageLoader.getInstance().into(context, info.logo, stationLogo)
        stationName.text = info.name
        stationAddress.text = info.adress
        distance.text = "${info.distance.distanceFormat()}"
    }

    fun updateInfo(oilNum: OilNum) {
        oldPrice.text = SpannableUtils.getSpannable("油站价 ¥${oilNum.oldPrice}", 0)
        gasPrice.text = SpannableUtils.getDiscountPrice("¥${oilNum.price}")
        couponView.setText("¥${oilNum.oldPrice.toBigDecimal().minus(oilNum.price.toBigDecimal()).toString()}")
    }

    private fun selectMap(lat: Double, lng: Double) {
        val dialog = SelectMapDialog(context, lat, lng)
        dialog.show()
    }

}